clear all;
%% Code explanation
% 170629 DZ  plot the calculated bands with Lorentzian broadening
% 190809 DZ  edit the old code to plot newly calculated bands
%% set parameters
kT=1.38E-23*300/1.6E-19;
E=(-3:0.005:3)';
k=(0:0.0005:0.345)';
%% Read the data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
root=('D:\Documents2\Matlab\data_analysis\Sn\190809 DFT bands\'); %
filenames={'2Sn_wo_PbTe';'2Sn_PbTe';'5Sn_wo_PbTe';'5Sn_PbTe'};

file1=[root,filenames{4},'.dat'];
            fid=fopen(file1);
            data1 = textscan(fid,'%f %f %f %f %f','commentStyle','k');    
            %%%                   k  E  s   pxy pz
            data1=data1';
            fclose(fid);
            KKraw=data1{1};
            EEraw=data1{2};
            SSraw=data1{3};
            PPxyraw=data1{4};
kraw=KKraw(1:180);
[k1,i1,ii1]=unique(kraw); % there exist two places in kraw where two data points have the same value.
NN=length(KKraw);

Epos=zeros(NN/180,length(k));
ws=zeros(NN/180,length(k));
wpxy=zeros(NN/180,length(k));
for kk=1:(NN/180)
    Eraw=EEraw((1+(kk-1)*180):(180+(kk-1)*180));
    wsraw=SSraw((1+(kk-1)*180):(180+(kk-1)*180));
    wpxyraw=PPxyraw((1+(kk-1)*180):(180+(kk-1)*180));
    
    Epos(kk,:)=interp1(kraw(i1),Eraw(i1),k);
    ws(kk,:)=interp1(kraw(i1),wsraw(i1),k);
    wpxy(kk,:)=interp1(kraw(i1),wpxyraw(i1),k);
end

%% calculate the intensities of the bands

Is=zeros(length(E),length(k));
Ipxy=zeros(length(E),length(k));
for ii=1:length(k)
    for jj=1:(NN/180)
        Is(:,ii)=Is(:,ii)+ws(jj,ii)./((E-Epos(jj,ii)).^2/kT^2+1);
        Ipxy(:,ii)=Ipxy(:,ii)+wpxy(jj,ii)./((E-Epos(jj,ii)).^2/kT^2+1);
    end
end

%%
figure
surf(k,E,Is,'EdgeColor','none');
shading interp
colorMap = gray(64); 
colormap(colorMap);
 axis([0,max(k),-1,1]);  
 
 figure
surf(k,E,Ipxy,'EdgeColor','none');
shading interp
colorMap = gray(64); 
colormap(colorMap);
 axis([0,max(k),-1,1]);  
 
 figure
 for ii=1:(NN/180)
   plot(k,Epos(ii,:),'-k');
   hold on
 end
  axis([0,max(k),-1,1]); 
%% End




